package com.web.dao.impl;


import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.web.dao.CartDao;
import com.web.domain.CartItem;
import com.web.utils.DB;

public class CartDaoImpl implements CartDao {
	
	private DB db = new DB();
	/* 
	 * 把购物车信息保存到数据库
	 * @see com.web.dao.CartDao#save(com.web.domain.CartItem)
	 */
	@Override
	public void save(CartItem cartItem) throws Exception {
		// TODO Auto-generated method stub
		String sql = "insert into cart values(?,?,?,?)";
		Object[] params = { cartItem.getCt_id(),cartItem.getCt_uid(),cartItem.getCt_pid(),cartItem.getCt_count() };
		db.doPstm(sql, params);
		db.closed();
	}
	
	/*
	 * 获取购物车信息
	 * @see com.web.dao.CartDao#getCart(java.lang.String)
	 */
	@Override
	public List<CartItem> getCart(String ct_uid) throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		String sql = "select * from cart where ct_uid=?";
		Object[] params = { ct_uid };
		db.doPstm(sql, params);
		ResultSet rs = db.getRs();
		List<CartItem> list = new ArrayList<>();
		while(rs.next()) {
			CartItem c =new CartItem();
			c.setCt_count(rs.getInt("ct_count"));
			c.setCt_id(rs.getString("ct_id"));
			c.setCt_pid(rs.getString("ct_pid"));
			c.setCt_uid("ct_pid");
			list.add(c);
		}
		db.closed();
		return list;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.web.dao.CartDao#getDateCount(java.lang.String, java.lang.String)
	 */
	@Override
	public CartItem getDateCount(String activeUser, String pid) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from cart where ct_uid=? and ct_pid=?";
		
		Object[] params = { activeUser,pid };
		db.doPstm(sql, params);
		ResultSet rs = db.getRs();
		CartItem c =new CartItem();
		while(rs.next()) {
			c.setCt_id(rs.getString("ct_id"));
			c.setCt_pid(rs.getString("ct_pid"));
			c.setCt_uid(rs.getString("ct_uid"));
			c.setCt_count(rs.getInt("ct_count"));
		}
		rs.close();
		db.closed();
		return c;
		
	}

	/*
	 * 通过id修改数量
	 * @see com.web.dao.CartDao#reviseByUid(int, java.lang.String)
	 */
	@Override
	public void reviseByUid(int up_Count,String ct_id) throws Exception {
		// TODO Auto-generated method stub		
		String sql = "update cart set ct_count=? where ct_id=? limit 1";
		Object[] params = { up_Count,ct_id };
		db.doPstm(sql, params);
		db.closed();
	}
	
	/*
	 * 通过id删除商品
	 * @see com.web.dao.CartDao#deleteById(java.lang.String)
	 */
	@Override
	public void deleteById(String ct_id) throws Exception {
		// TODO Auto-generated method stub
		String sql = "delete from cart where ct_id=?";
		Object[] params = { ct_id };
		db.doPstm(sql, params);
		db.closed();
	}
	/*
	 * 清空购物车
	 * @see com.web.dao.CartDao#deleteAll(java.lang.String)
	 */
	@Override
	public void deleteAll(String ct_uid) throws Exception {
		// TODO Auto-generated method stub
		String sql = "delete from cart where ct_uid=?";
		Object[] params = { ct_uid };
		db.doPstm(sql, params);
		db.closed();
	}

	@Override
	public CartItem getDataSub(String ct_id) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from cart where ct_id=? limit 1";

		Object[] params = { ct_id };
		db.doPstm(sql, params);
		ResultSet rs = db.getRs();
		CartItem c =new CartItem();
		while(rs.next()) {
			c.setCt_count(rs.getInt("ct_count"));
			c.setCt_id(rs.getString("ct_id"));
			c.setCt_pid(rs.getString("ct_pid"));
			c.setCt_uid(rs.getString("ct_uid"));
		}
		return c;
	}

}
